var number=1;
var xcountIntegral = 0;
var dkFee = 0;
var countprice;

function add() {
	number = $("#number").html();
	number++;
	$("#number").html(number);
	$("#storeInfo_number").html("x" + number);
    totalFee = price*number;
    countprice =  price*number-xcountIntegral;
    $("#total_number").html("￥" +countprice);
    var khdScoreCount = khdScore *  number;
    $("#hdScore").html("可获得积分:"+khdScoreCount);
}

function subtract() {
	number = $("#number").html();
	if(number<=1){
		return;
	}
	number--;
	$("#number").html(number);
	$("#storeInfo_number").html("x" + number);
    totalFee = price*number;
    countprice =  price*number-xcountIntegral;
    $("#total_number").html("￥" +countprice);

    var khdScoreCount = khdScore *  number;
    $("#hdScore").html("可获得积分:"+khdScoreCount);
}

function sure_pay() {
    var selectedAddress = JdataGet('selectedAddress');
        if(!selectedAddress) {
            alert("请先输入你的收货地址！");
            return;
        }
    var r = confirm("确定购买此商品");
    if (r==false) {
        return;
    }

    layer.open({type: 2});
    sureToPay();
}

function addNewAddress() {
    nextView("../store/addressManage.html");  
}

var khdScore;
var imgUrlID;
var price;
$(function(){
    khdScore = GetParpam("khdScore");
    imgUrlID = GetParpam("imgUrlID");
    price = GetParpam("price");
    totalFee = price;
    countprice = price;
    viewsScore();

    $('#goods_detail_pic').css("background","url(" + urlfile + "coreAttachment/image/get/" + imgUrlID + ")  no-repeat;-webkit-background-size: 88px 78px;background-size: 88px 78px;");
    $("#unit_price").html("￥" + price);
    $("#total_number").html("￥" + price);
    $("#hdScore").html("可获得积分:"+khdScore);

    
	var userUuid = dataGet("userUuid");
	var data= "&crarsUser=" + userUuid;
    var url = urlfile + "coreAddress/find/my/address";

    ajaxTool("post",data,url,
        function error(XMLHttpRequest, textStatus, errorThrown,fnErr){
            alert("error:" + XMLHttpRequest);
        },
        function success(data){
            if(!data.success) {
                alert(data.errMsg);
            }else{
            	if(data.data && data.data.length>0){
            		var select = dataGet("addressIndex");
            		if(select==null || select>data.data.length){
            			select=0;
            		}
            		JdataSave('selectedAddress',data.data[select]);
            		$('#name').html("收货人："+ data.data[select].crarsName);
                    crporName = data.data[select].crarsName;
                    $("#hdScore").html("可获得积分:"+khdScore);
            		$('#phoneNo').html(data.data[select].crarsTel);
                    crporTel = data.data[select].crarsTel;
            		$('#address').html("收货地址："+data.data[select].crarsCity+data.data[select].crarsAddress);
                    crporCity = data.data[select].crarsCity;
                    crporAddress = data.data[select].crarsAddress;
            	}else{
					$("#pay_address").html('<span style="color: #b4b4b4;line-height: 97px;">您还没有收货地址，</span><span style="color: #ffae00;">请添加地址</span>');
            	}
            }
        }
    );
})

var crporName;
var crporTel;
var crporCity;
var crporAddress;
var totalFee;
function sureToPay() {
    var syScore = parseInt($("#integralInput").val());
    var userUuid = dataGet("userUuid");
    var openId = dataGet("openId");
    var crporGoodsUuid = GetParpam("crpopUuid");
    var goodName = dataGet("goodName");
    var body = "商品购买";

    var crporRemarks = $("#store_remark").val();
    var data = "&crporGoodsUuid="+crporGoodsUuid+"&crporGoodsName="+goodName+"&crporName="+crporName+"&crporTel="+crporTel+"&dkFee="+dkFee
    +"&userUuid="+userUuid+"&body="+body+"&openId="+openId +"&attach=2"
    +"&crporCity="+crporCity+"&crporAddress="+crporAddress+"&zfFee="+countprice +"&crporNumber="+number
    +"&totalFee="+totalFee+"&syScore="+syScore+"&crporPrice="+price +"&crporRemarks="+crporRemarks +"&uuids="+ imgUrlID;
    var url = urlfile + "pay/get/package/shangpin";
    ajaxTool("post",data,url,
        function error(XMLHttpRequest, textStatus, errorThrown,fnErr){
            alert("error:" + data);
        },
        function success(data){
            if(!data.success) {
                alert(data.errMsg);
            }else{
                goToPay(data);
            }
        }
    );
}

function viewsScore(){
    $('#price').html("￥" + price);
    $('#total_number').html("￥" + price);

    var crusrUuid = dataGet("userUuid");
    var data = "&crusrUuid="+crusrUuid;
    var url = urlfile + "coreUser/views/score";
    ajaxTool("post",data,url,
        function error(XMLHttpRequest, textStatus, errorThrown,fnErr){
            alert("error:" + data);
        },
        function success(data){
            if(!data.success) {
                alert(data.errMsg);
            }else{
                crusrPoints = data.data.crusrPoints;
                $('#crusrPoints').html('你有'+ crusrPoints +'积分');
                
                jfdkbl = data.data.jfdkbl;
            }
        }
    );
}

function countIntegral() {
    var integralInput = parseInt($("#integralInput").val());
    if(integralInput>crusrPoints){
        integralInput = crusrPoints;
        $("#integralInput").val(integralInput);
    }
     xcountIntegral = integralInput*jfdkbl;
     dkFee = xcountIntegral;
    $('#span_integral').html("-￥" + xcountIntegral);
    countprice = price*number - xcountIntegral;
    $('#total_number').html("￥" + countprice);
}